package com.itheima.controller.pms;


import com.itheima.core.PageInfo;
import com.itheima.core.R;
import com.itheima.pojo.pms.PmsProductAttribute;
import com.itheima.pojo.pms.vo.ProductAttrInfoVo;
import com.itheima.service.pms.PmsProductAttributeService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品属性管理Controller
 */
@RestController
@RequestMapping("/productAttribute")
public class PmsProductAttributeController {
    @DubboReference
    private PmsProductAttributeService pmsProductAttributeService;


    /**
     * 根据商品分类的id获取商品属性及属性分类
     *
     * @param productCategoryId
     * @return
     */
    @GetMapping("attrInfo/{productCategoryId}")
    public R getAttrInfo(@PathVariable Long productCategoryId) {
        List<ProductAttrInfoVo> productAttrInfoList = pmsProductAttributeService.getProductAttrInfo(productCategoryId);
        return R.success(productAttrInfoList);
    }

    /**
     * 添加商品属性信息
     *
     * @param pmsProductAttribute
     * @return
     */
    @PostMapping("create")
    public R create(@RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttributeService.save(pmsProductAttribute);
        return R.success();
    }

    /**
     * 批量删除商品属性
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public R delete( @RequestParam("ids")  List<Long> ids) {
        pmsProductAttributeService.removeByIds(ids);
        return R.success();
    }


    /**
     * 修改商品属性信息
     *
     * @param id
     * @param pmsProductAttribute
     * @return
     */
    @PostMapping("update/{id}")
    public R update(@PathVariable Long id, @RequestBody PmsProductAttribute pmsProductAttribute) {
        PmsProductAttribute pmsProductAttribute1 = new PmsProductAttribute();
        pmsProductAttribute1.setId(id);
        BeanUtils.copyProperties(pmsProductAttribute, pmsProductAttribute1);
        pmsProductAttributeService.updateById(pmsProductAttribute1);
        return R.success();
    }

    /**
     * 查询单个商品属性
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R getById(@PathVariable Long id) {
        return R.success(pmsProductAttributeService.getById(id));
    }

    /**
     * 根据分类查询属性列表或参数列表
     * @param cid
     * @param type
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("list/{cid}")
    public R getList(@PathVariable Long cid,
                     @RequestParam(value = "type") Integer type,
                     @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {

        PageInfo productAttributeList = pmsProductAttributeService.getList(cid, type, pageSize, pageNum);

        return R.success(productAttributeList);
    }


}



